<?php
if(!isset($in_pmt)) header("Location:index.php");
/*$date = explode("-",$today);
$day = $date[2];
$month = $date[1];
$year = $date[0];
next_month_year($month,$year,$next_month,$prev_month,$next_year,$prev_year);
$prev_title = date("M",mktime(0,0,0,$prev_month,1,$prev_year));
$next_title = date("M",mktime(0,0,0,$next_month,1,$next_year));
*/
$id = (int) $_GET["id"];

$query = $db->query("select * from products where pid = $id");
$days_in_month = cal_days_in_month(0,$date->curr_month,$date->curr_year);
if($db->num_rows($query)==0){
	$msg->error("Няма такъв продукт!");
} else {
	$res = $db->fetch_assoc($query);
	$msg->write("<div id=\"info\">Информация за продукт: <strong>".$res["product"]."</strong></div>");
	$msg->write("<div id=\"list\">");
	$msg->write("<span>Разходи за месец {$date->curr_month_title} {$date->curr_year} | 
	<a href=\"?p=product&amp;id=$id&amp;date={$date->prev_year}-{$date->prev_month}-01\">Разходи за месец {$date->prev_month_title} {$date->prev_year}| 
	<a href=\"?p=product&amp;id=$id&amp;date={$date->next_year}-{$date->next_month}-01\">Разходи за месец {$date->next_month_title} {$date->next_year}</a>  
	</span>");
	$msg->write("<ul>");
	$query = $db->query("SELECT * FROM uses JOIN products ON uses.product_id = products.pid where uses.date like '{$date->curr_year}-{$date->curr_month}-%' and uses.product_id = $id");
	$sum= 0;
	if($db->num_rows($query) >0){
		
		while($row = $db->fetch_assoc($query)){
			$msg->write("<li><a href=\"?p=day&amp;date=".$row['date']."\">".$row['date']."</a> : ".split_price($row["price"])." лв. </li>");
			$sum +=  $row["price"];
			$month_data[$row['date']] = $row['price'];
		}
		$msg->write("</ul>");
		$msg->write("</div>");
		$msg->write("<div id=\"info\"> Разходи за месеца: <strong>".split_price($sum)."лв.</strong> </div>");
		for($i=1;$i<=$days_in_month;$i++){
			if($i<10){
				$day ="0".$i;
			} else {
				$day = $i;
			}
			if(array_key_exists($date->curr_year."-".$date->curr_month."-".$day,$month_data)){
				$data .=(($i-1)."=".split_price($month_data[$date->curr_year."-".$date->curr_month."-".$day])."&amp;");
			} else {
				$data .=(($i-1)."=0.0&amp;");
			}
		}
		echo '<img src="graph_image.php?'.$data.'" />';
	} else { 
		
		$msg->write("<li>Няма разходи за този месец!</li>");
		$msg->write("</div>");
	}
}
?>